package com.ccit.springbootlaw.controller.base;

import com.ccit.springbootlaw.controller.resful.Code;
import com.ccit.springbootlaw.controller.resful.Result;
import com.ccit.springbootlaw.pojo.CaseLawRelationship;
import com.ccit.springbootlaw.service.CaseLawRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/case-law-relationships")
public class CaseLawRelationshipController {

    @Autowired
    private CaseLawRelationshipService caseLawRelationshipService;

    /**
     * 根据ID查找案件与法律关系
     *
     * @param id 关系ID
     * @return 返回找到的关系对象，如果未找到则返回错误信息
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable long id) {
        CaseLawRelationship relationship = caseLawRelationshipService.findById(id);
        if (relationship != null) {
            return new Result(Code.GET_OK, relationship, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "未找到对应关系");
        }
    }

    /**
     * 根据案件ID查找相关法律关系
     *
     * @param caseId 案件ID
     * @return 返回与该案件相关的法律关系列表
     */
    @GetMapping("/case/{caseId}")
    public Result findByCaseId(@PathVariable int caseId) {
        List<CaseLawRelationship> relationships = caseLawRelationshipService.findByCaseId(caseId);
        if (!relationships.isEmpty()) {
            return new Result(Code.GET_OK, relationships, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "未找到相关法律关系");
        }
    }

    /**
     * 根据法律ID查找相关案件关系
     *
     * @param lawId 法律ID
     * @return 返回与该法律相关的案件关系列表
     */
    @GetMapping("/law/{lawId}")
    public Result findByLawId(@PathVariable int lawId) {
        List<CaseLawRelationship> relationships = caseLawRelationshipService.findByLawId(lawId);
        if (!relationships.isEmpty()) {
            return new Result(Code.GET_OK, relationships, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "未找到相关案件关系");
        }
    }

    /**
     * 插入新的案件与法律关系
     *
     * @param relationship 关系对象
     * @return 返回插入操作的结果
     */
    @PostMapping
    public Result insertCaseLawRelationship(@RequestBody CaseLawRelationship relationship) {
        int result = caseLawRelationshipService.insertCaseLawRelationship(relationship);
        if (result > 0) {
            return new Result(Code.ADD_OK, relationship, "添加成功");
        } else {
            return new Result(Code.ADD_ERR, null, "添加失败");
        }
    }

    /**
     * 根据ID删除案件与法律关系
     *
     * @param id 关系ID
     * @return 返回删除操作的结果
     */
    @DeleteMapping("/{id}")
    public Result deleteCaseLawRelationshipById(@PathVariable long id) {
        int result = caseLawRelationshipService.deleteCaseLawRelationshipById(id);
        if (result > 0) {
            return new Result(Code.DELETE_OK, null, "删除成功");
        } else {
            return new Result(Code.DELETE_ERR, null, "删除失败");
        }
    }

    /**
     * 更新案件与法律关系
     *
     * @param relationship 关系对象
     * @return 返回更新操作的结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody CaseLawRelationship relationship) {
        int result = caseLawRelationshipService.updateCaseLawRelationship(relationship);
        if (result > 0) {
            return new Result(Code.UPDATE_OK, relationship, "更新成功");
        } else {
            return new Result(Code.UPDATE_ERR, null, "更新失败");
        }
    }

    /**
     * 查询所有案件与法律关系
     *
     * @return 返回所有案件与法律关系的列表
     */
    @GetMapping
    public Result findAll() {
        List<CaseLawRelationship> relationships = caseLawRelationshipService.findAll();
        if (!relationships.isEmpty()) {
            return new Result(Code.GET_OK, relationships, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "未找到任何关系");
        }
    }
}